<?php

namespace App\Services;

use App\Models\WxMenu;
use App\Models\WxReply;
use App\Services\WechatApi\WechatApiOffiAccount;

class WxMenuService extends ServiceBase
{
    public function getList($appid)
    {
        $list = WxMenu::where('appid', $appid)->orderByRaw('sort desc, id asc')->get()->toArray();
        $arr  = [];
        foreach ($list as $item) {
            $id  = $item['id'];
            $pid = $item['pid'];
            if ($pid == 0) {
                $arr[$id]        = $item;
                $arr[$id]['sub'] = [];
                continue;
            }
            $arr[$pid]['sub'][] = $item;
        }

        return $arr;
    }

    public function createMenu($appid)
    {
        $params = $this->handleMenuData($appid);
        if (empty($params)) {
            return 7018;
        }

        $wx_config = WxConfigService::getInstance()->one($appid);
        if (!$wx_config) {
            return 7019;
        }

        $access_token = WechatService::getInstance()->getAccessToken($wx_config->appid, $wx_config->secret);
        if (!$access_token) {
            return 7020;
        }

        $res = WechatApiOffiAccount::getInstance()->menuCreate($access_token, $params);
        if (!$res) {
            return 7011;
        }

        return 0;
    }

    public function handleMenuData($appid)
    {
        $data = [];
        $list = $this->getList($appid);
        if (!empty($list)) {
            foreach ($list as $item) {
                if (!empty($item['sub'])) {
                    $data['button'][] = [
                        'name'       => $item['name'],
                        'sub_button' => $this->buildSubMenu($item['sub']),
                    ];
                } else {
                    $data['button'][] = $this->buildMenu($item);
                }
            }

            $data['button'] = array_filter($data['button'], function ($item) {
                return !empty($item);
            });
        }

        return $data;
    }

    public function buildSubMenu($sub_arr)
    {
        $data = [];
        foreach ($sub_arr as $item) {
            $data[] = $this->buildMenu($item);
        }

        return $data;
    }

    public function buildMenu($item)
    {
        $data = [];
        switch ($item['type']) {
            case WxMenu::MENU_TYPE_MSG:
                $reply = WxReply::where('id', $item['wx_reply_id'])->first();
                $data = [
                    'type' => 'click',
                    'name' => $item['name'],
                    'key'  => $reply->event_key ?? '',
                ];
                break;
            case WxMenu::MENU_TYPE_WEB:
                $data = [
                    'type' => 'view',
                    'name' => $item['name'],
                    'url'  => $item['url'],
                ];
                break;
            case WxMenu::MENU_TYPE_MINI:
                $data = [
                    'type'     => 'miniprogram',
                    'url'      => $item['url'],
                    'appid'    => $item['appid'],
                    'pagepath' => $item['pagepath'],
                ];
                break;
            default:
                break;
        }

        return $data;
    }
}
